HTTP.ino
#include "HTTP.h"
void send_POST(char* Line)
{
if(WiFi.status()== WL_CONNECTED)
{
//HTTPClient http;
// Your Domain name with URL path or IP address with path
String httpRequestData = "api_key=" + Ampel_state_apiKey +"&el_1=" + (String)Line[4]+"&el_2=" + (String)Line[5]+"&el_3=" + (String)Line[6]+"&el_4=" + (String)Line[7]+"&el_5=" + (String)Line[8];
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//Serial.print("\nhttpRequestData: ");
//Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = 0;
httpResponseCode = http.POST(httpRequestData);
String payload = http.getString();
if (httpResponseCode>0)
{
Serial.println("Sent");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
//Serial.print("Payload: ");
//Serial.println(payload);
}
else {
Serial.println("Not sent");
//Serial.print("Error code: ");
//Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else
{
//Serial.println("No WiFi connection\nESP-01 is Creating an Acess Point");
//Serial.println("Network Name: " + (String)AccessPoint_name);
//Serial.println("Network Password: " + (String)AccessPoint_password);
//WiFi.softAP(AccessPoint_name, AccessPoint_password);
Status.WiFi_Connected = 0;
digitalWrite(2, HIGH); // WiFi disconnected! Turn OFF the LED
}
}
void Get_Ext_IP(char* Line, char* adress)
{
WiFiClient client;
TextFinder finder(client);
if (client.connect(global_IP_Server, 80))
{
//Serial.println("Connected - Acquiring WAN IP:");
//make HTTP request
client.println("GET /");
client.println();
delay(1000);
}//end if
else
{ // you didn't get a connection to the server:
Serial.println("connection failed");
}//end else
while (client.available())
{
char c = client.read();
//Serial.print("First readed shit: ");
//Serial.println(c);
if (client.available())
{
if(finder.find("IP Address: "))
{
for (char k = 0; k < 17; k++)
{
c = client.read();
if(c != '<')
{
//Serial.print(c);
Line[k] = c;
} //endif
else
break;
}//end for
}//end if
}//end if
client.flush();
client.stop();
}
}
void POST_Ext_IP(const char* Site_Address, char* IP_address, char* password)
{
if(WiFi.status()== WL_CONNECTED)
{
//HTTPClient http;
String httpRequestData = "api_key2=" + (String)password +"&Modul_address=" + "http://" + (String)IP_address + "/SET";
//String httpRequestData = "api_key2=" + (String)password +"&Modul_address=" + "http://" + (String)IP_address;
//String httpRequestData = "api_key2=" + (String)password +"&Modul_address=" + (String)IP_address;
http.begin(Site_Address);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
Serial.print("\nhttpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = 0;
httpResponseCode = http.POST(httpRequestData);
String payload = http.getString();
if (httpResponseCode>0)
{
Serial.println("External IP Sent");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
//Serial.print("Payload: ");
//Serial.println(payload);
}
else
{
Serial.println("External IP WASN`T sent");
//Serial.print("Error code: ");
//Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else
{
//Serial.println("No WiFi connection\nESP-01 is Creating an Acess Point");
//Serial.println("Network Name: " + (String)AccessPoint_name);
//Serial.println("Network Password: " + (String)AccessPoint_password);
//WiFi.softAP(AccessPoint_name, AccessPoint_password);
Status.WiFi_Connected = 0;
digitalWrite(2, HIGH); // WiFi disconnected! Turn OFF the LED
}
}
void Get_time(status_flags_t* Status)
{
String Date_and_time;
/***************************** TIME SYNCHRONISATION PROCESS *****************************/
if(WiFi.status()== WL_CONNECTED)
{
WiFiUDP ntpUDP;
const long utcOffsetInSeconds = 60*60; // for germany (GMT+1 = 60min * 60 sec)
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
//unsigned long epochTime = timeClient.getEpochTime();
timeClient.begin();
delay(500);
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
String formattedTime = timeClient.getFormattedTime();
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;
int currentYear = ptm->tm_year+1900;
//formattedTime = timeClient.getFormattedTime();
if(currentYear < 2000)
Date_and_time = "NOWIFI"; // Condition to prevent situations when connection with router is created, but router is not connected to the internet
else
Date_and_time = String(formattedTime) + ":" + String(currentYear) + ":" + String(currentMonth) + ":" + String(monthDay);
}
else
{
Date_and_time = "NOWIFI";
//Serial.println("No WiFi connection\nESP-01 is Creating an Acess Point");
//Serial.println("Network Name: " + (String)AccessPoint_name);
//Serial.println("Network Password: " + (String)AccessPoint_password);
//WiFi.softAP(AccessPoint_name, AccessPoint_password);
Status->WiFi_Connected = 0;
digitalWrite(2, HIGH); // WiFi disconnected! Turn OFF the LED
}
Serial.println(Date_and_time);
/********************************************************************************************/
}
void Get_Ampel_states(status_flags_t* Status, const char* address)
{
String Site_response;
i = 0;
//WiFiClient client;
HTTPClient http;
//TextFinder finder(client);
//Serial.println("Connecting to " + (String)address);
//if (client.connect(address, 80))
if(WiFi.status()== WL_CONNECTED)
{
http.begin(address);
http.GET();
Site_response = http.getString();
if(Site_response != "")
Serial.println(Site_response);
http.end();
}
else
{
Status->WiFi_Connected = 0;
digitalWrite(2, HIGH); // WiFi disconnected! Turn OFF the LED
}
//while (client.available())
//{
///****************** PARSING THE RECEIVED DATA *********************/
// }
}